{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `scikit-geometry` package contains a whole lot of geometric algorithms. For the moment, `scikit-geometry` derives most of it's functionality from the mature and stable Computational Geometry Algorithms Library (`CGAL`) package, a collection of geometric algorithms written in C++.\n",
    "\n",
    "## Basic Geometric Types\n",
    "\n",
    "We aim to cleanly encapsulate the important building blocks of a geometric library. As such we have implementations for the basic geometric types:\n",
    "\n",
    "- Point2, Point3\n",
    "- Segment2, Ray2, Line2, Segment3, Ray3, Line3\n",
    "- Vector2, Vector3\n",
    "- Circle2\n",
    "- IsoRectangle2, Bbox2\n",
    "- Plane3, Triangle3\n",
    "- Polyhedron3\n",
    "\n",
    "All sorts of operations can be performed with these basic building blocks. For example, one can construct a new segment from two points, then test against intersection with another segment, or a line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import skgeom as sg\n",
    "a = sg.Point2(5, 3)\n",
    "b = sg.Point2(10, 8)\n",
    "print(a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can create a vector from two points by subtracting one from another."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = a - b\n",
    "print(v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or a segment between two points, where `a` is the source and `b` the target."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = sg.Segment2(a, b)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And a ray from a point and a vector which indicates the direction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = sg.Ray2(a, v)\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Finding intersections\n",
    "\n",
    "It's possible to find intersections between two geometric primitives by using the intersection function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = sg.Segment2(sg.Point2(3, 5), sg.Point2(0, -2))\n",
    "b = sg.Segment2(sg.Point2(5, 3), sg.Point2(-2, -2))\n",
    "i = sg.intersection(a, b)\n",
    "print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: depending on the type of intersection, the return type could also be `None` or `Segment2` if there is a complete overlap between the two elements."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Drawing functions\n",
    "\n",
    "We have functionality to draw some of the geometric primitives presented here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from skgeom.draw import draw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "draw(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import random as r\n",
    "import itertools\n",
    "segments = []\n",
    "for i in range(10):\n",
    "    segments.append(sg.Segment2(sg.Point2(r(), r()), \n",
    "                                sg.Point2(r(), r())))\n",
    "\n",
    "intersections = []\n",
    "for s1, s2 in itertools.permutations(segments, 2):\n",
    "    isect = sg.intersection(s1, s2)\n",
    "    if isect:\n",
    "        intersections.append(isect)\n",
    "\n",
    "for s in segments:\n",
    "    draw(s)\n",
    "for i in intersections:\n",
    "    draw(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Further functionality\n",
    "\n",
    "Of course, scikit-geometry comes with much more functionality.\n",
    "\n",
    "- Compute convex hulls from points\n",
    "- Visibility polygons from two-dimensional arrangements\n",
    "- Compute the Minkowski sum of two polygons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
